package com.example;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Queue;
import javax.jms.Topic;
import javax.servlet.MultipartConfigElement;

@EnableAsync
@RestController
@SpringBootApplication        // @SpringBootApplication = @Configuration  + @EnableAutoConfiguration  + @ComponentScan
//spring能够扫描到我们自己编写的servlet和filter。
//@ServletComponentScan  //servlet注册2

//可以使用：basePackageClasses={},basePackages={}  要设置对,否则其他目录的controller不能访问
//@ComponentScan(basePackages={"com.example.scan.pack1","com.example"})
//@MapperScan("com.example.*.*Mapper")
public class DemoApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(this.getClass());
    }

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }

    /**
     * activemq 消息订阅
     * @return
     */
    //spring.jms.pub-sub-domain=false  或者不加该配置
    @Bean
    public Queue queue() {
        return new ActiveMQQueue("sample.queue");
    }

    //spring.jms.pub-sub-domain=true
    @Bean
    public Topic topic() {
        return new ActiveMQTopic("sample.topic");
    }
//
//    //启动器注册接收者
//    @Bean
//    public org.springframework.amqp.core.Queue fooQueue(){
//        return new org.springframework.amqp.core.Queue("foo");
//    }

    public static void main(String[] args) {
        System.out.println("----------> hello_demo ");

       /*
         * Banner.Mode.OFF:关闭;----不行
         * Banner.Mode.CONSOLE:控制台输出，默认方式;
         * Banner.Mode.LOG:日志输出方式;
         */
//        SpringApplication application = new SpringApplication(DemoApplication.class);
//        application.setBannerMode(Banner.Mode.OFF);
//        application.run(args);

        //获取context.
        ApplicationContext ctx =  SpringApplication.run(DemoApplication.class, args);
//        String[] beanNames =  ctx.getBeanDefinitionNames();
//        System.out.println("---->beanNames个数："+beanNames.length);
//        for(String bn:beanNames){
//            System.out.println("--->bean : "+bn);
//        }

       /*
       //动态注入
       //获取BeanFactory
        DefaultListableBeanFactory defaultListableBeanFactory
                = (DefaultListableBeanFactory) ctx.getAutowireCapableBeanFactory();
        //创建bean信息.
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(TestService.class);
        beanDefinitionBuilder.addPropertyValue("name","张三");

        //多次注入同一个bean的，如果beanName不一样的话，那么会产生两个Bean；如果beanName一样的话，后面注入的会覆盖前面的。
        beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(TestService.class);
        beanDefinitionBuilder.addPropertyValue("name","李四");

        //动态注册bean.
        defaultListableBeanFactory.registerBeanDefinition("testService",
                beanDefinitionBuilder.getBeanDefinition());

        defaultListableBeanFactory.registerBeanDefinition("testService1",
                beanDefinitionBuilder.getBeanDefinition());

        defaultListableBeanFactory.removeBeanDefinition("testService");

        TestService testService = ctx.getBean(TestService.class);
        testService.print();*/

//		SpringApplication.run(DemoApplication.class, args);
//		SpringApplication.run(DemoApplication.class, new String[]{"hello,","林峰"});

//		new SpringApplicationBuilder() .showBanner(true).sources(DemoApplication.class).run(args);
//
//		new SpringApplicationBuilder(DemoApplication.class).showBanner(false).run(args);
//
//		SpringApplication application = new SpringApplication(DemoApplication.class);
//		application.setBanner(null);
//		application.run(args);

    }

    /**
     * 对文件做一些限制是有必要的，在App.java进行编码配置： 注册servlet
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //// 设置文件大小限制 ,超了，页面会抛出异常信息，这时候就需要进行异常信息的处理了;
        factory.setMaxFileSize("128KB"); //KB,MB
        /// 设置总上传数据总大小
        factory.setMaxRequestSize("256KB");
        //Sets the directory location where files will be stored.
        //factory.setLocation("路径地址");
        return factory.createMultipartConfig();
    }


    /**
     * 注册Servlet.不需要添加注解：@ServletComponentScan  servlet注册1
     *
     * @return
     */
//    @Bean
//    public ServletRegistrationBean MyServlet1() {
//        return new ServletRegistrationBean(new MyServlet1(), "/myServlet/*");
//    }
    //注册Servlet.不需要添加注解：@ServletComponentScan


    /**
     * SSL
     server.ssl.key-store=.keystore
     server.ssl.key-store-password=admins
     server.ssl.key-store-type=JKS
     server.ssl.key-alias=tomcat
     */
    //http自动转换为https .keystore
    //https://localhost:8888/demo/leaf   -->   https://localhost:8888/demo/leaf
//
//    @Bean
//    public EmbeddedServletContainerFactory servletContainer() {
//      TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
//          @Override
//          protected void postProcessContext(Context context) {
//              SecurityConstraint securityConstraint = new SecurityConstraint();
//              securityConstraint.setUserConstraint("CONFIDENTIAL");
//              SecurityCollection collection = new SecurityCollection();
//            collection.addPattern("/*");
//            securityConstraint.addCollection(collection);
//            context.addConstraint(securityConstraint);
//          }
//        };
//
//      tomcat.addAdditionalTomcatConnectors(httpConnector());
//      return tomcat;
//    }
//    @Bean
//    public Connector httpConnector() {
//      Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
//      connector.setScheme("http");
//      connector.setPort(8080);
//      connector.setSecure(false);
//      connector.setRedirectPort(8888);
//      return connector;
//	}

}









